using System.Collections;

/// <summary>
/// This base page addresses the following:
//-->Page expiration issue on the back button click.
//-->Unable to maintain the sort column & sort order selected by a user upon him/her navigating to another page.
//-->Improper navigation on the grid page. Steps to recreate
//1. Page throgh twice on the grid using "next page" link.(The grid shows the records 41-60(page 3 of the grid))
//2. Hit back button twice(The grid displays the records 1-20) 
//3. Hit "next page" on the grid, the grid displays the 4th page with records 61-80(instead of second page)
/// </summary>
namespace PHSRAG.Insight21.Common.Pages
{
    public class GridPage : CachePage
    {
        public GridPage()
        {
        }

        public GridPage(string cacheKey)
        {
            PageCacheKey = cacheKey;
        }

        #region Properties

        /// <summary>
        /// Gets the sortcolumn from the page cache policy
        /// </summary>
        public string SortColumn
        {
            get { return GetValue(Keys.GridBasePageSortColumnKey) as string; }
            set { SetValue(Keys.GridBasePageSortColumnKey, value); }
        }

        /// <summary>
        /// Gets the sortOrder from the page cache policy
        /// </summary>
        public bool SortAscending
        {
            get
            {
                object obj = GetValue(Keys.GridBasePageSortOrderKey);
                return (null == obj) ? false : (bool) obj;
            }
            set { SetValue(Keys.GridBasePageSortOrderKey, value); }
        }

        /// <summary>
        /// Gets the sortOrder from the page cache policy
        /// </summary>
        public int CurrentPageIndex
        {
            get
            {
                object obj = GetValue(Keys.GridBasePageCurrentPageIndexKey);
                return (null == obj) ? 0 : (int) obj;
            }
            set { SetValue(Keys.GridBasePageCurrentPageIndexKey, value); }
        }

        #endregion

        #region Private methods

        /// <summary>
        /// This method will fetch the values for passed in keys from the Hashtable(gridProperties) 
        /// available inside the pageCache 
        /// </summary>
        /// <param name="key"> key that identifies the object stored in pageCache hashtable</param>
        /// <returns>object value from the pagechache hastable</returns>
        private object GetValue(string key)
        {
            Hashtable gridProperties;

            if (PrevailingViewStateCachePolicy.GetCachedObject(PageCacheKey) == null)
            {
                gridProperties = PrevailingPageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
            }
            else
            {
                gridProperties = PrevailingViewStateCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
            }

            return (null != gridProperties && gridProperties.ContainsKey(key)) ? gridProperties[key] : null;
        }

        /// <summary>
        /// This method will fetch the values for passed in keys from the Hashtable(gridProperties) 
        /// available inside the pageCache 
        /// </summary>
        /// <param name="key"> key that identifies the object stored in pageCache hashtable</param>
        /// <param name="value"></param>
        /// <returns>object value from the pagechache hastable</returns>
        private void SetValue(string key, object value)
        {
            Hashtable gridProperties = PrevailingCachePolicy.GetCachedObject(PageCacheKey) as Hashtable ?? new Hashtable();
            gridProperties[key] = value;
            PrevailingPageCachePolicy.Cache(PageCacheKey, gridProperties);
            PrevailingViewStateCachePolicy.Cache(PageCacheKey, gridProperties);
        }

        #endregion
    }
}